package com.zyl.blog.controller.admin;/**
 * @author 14323
 * @date 2020/8/8 20:19
 */

import com.zyl.blog.pojo.Tag;
import com.zyl.blog.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;

/**
 * @Description: TODO
 * @author: scott
 * @date: 2020年08月08日 20:19
 */
@Controller
@RequestMapping("/admin")
public class TagController {

    @Autowired
    private TagService tagService;

    @GetMapping("/tags")
    public String tags(
            @PageableDefault(size = 5, sort = {"id"}, direction = Sort.Direction.DESC) Pageable pageable,
            Model model){
        Page<Tag> tagPage = tagService.getTagPage(pageable);
        model.addAttribute("tagPage",tagPage);
        return "admin/tags";
    }

    @GetMapping("/tags/input")
    public String input(Model model){
        model.addAttribute("tag", new Tag());
        return "/admin/tags-input";
    }

    @GetMapping("/tags/{id}/input")
    public String input(@PathVariable Long id, Model model){
        model.addAttribute("tag", tagService.getTag(id));
        return "/admin/tags-input";
    }

    @PostMapping("/tags/add")
    public String addTag(@Valid Tag tag, BindingResult result, RedirectAttributes attributes){
        //非重复验证
        Tag validTag = tagService.getTagByName(tag.getName());
        if (validTag != null){
            result.rejectValue("name", "nameError", "不能添加重复的标签！");
        }
        if (result.hasErrors()){
            return "/admin/tags-input";
        }

        //验证无误后进行添加操作
        Tag saveTag = tagService.saveTag(tag);
        if (saveTag == null){
            attributes.addFlashAttribute("message", "添加失败！");
        } else {
            attributes.addFlashAttribute("message", "添加成功！");
        }
        return "redirect:/admin/tags";
    }

    @PostMapping("/tags/update/{id}")
    public String updateTag(@PathVariable Long id, @Valid Tag tag, BindingResult result, RedirectAttributes attributes ){
        //非重复验证
        Tag validTag = tagService.getTagByName(tag.getName());
        if (validTag != null){
            result.rejectValue("name", "nameError", "请输入新标签的名称！");
        }
        if (result.hasErrors()){
            return "/admin/tags-input";
        }

        //验证无误后进行更新操作
        Tag updateTag = tagService.updateTag(id, tag);
        if (updateTag == null){
            attributes.addFlashAttribute("message", "更新失败！");
        } else {
            attributes.addFlashAttribute("message", "更新成功！");
        }

        return "redirect:/admin/tags";
    }

    @GetMapping("/tags/{id}/delete")
    public String deleteTag(@PathVariable Long id, RedirectAttributes attributes){
        tagService.deleteTag(id);
        attributes.addFlashAttribute("message", "删除成功！");

        return "redirect:/admin/tags";
    }

}
